<?php
/**
 * Copyright (c) [2019] [吴跃忠]
 * [selibra] is licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 * http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 */

namespace Selibra\Redis;


use Co\Redis;
use Swoole\Coroutine;

class RedisClient
{

    /**
     * @var Redis
     */
    protected Redis $redis;


    /**
     * 协程连接
     * @var array
     */
    protected static array $coIns = [];


    /**
     * @var array
     */
    protected array $config = [];


    /**
     * RedisClient constructor.
     * @param array $config
     */
    public function __construct($config = [])
    {
        $this->config = $config;
    }


    /**
     * @return Redis|mixed
     */
    public function redis()
    {
//        if (!isset($this->redis)) {
//            $this->redis = $this->getRedisIns($this->config);
//        }
        return $this->getRedisIns($this->config);
    }


    /**
     * @param null $config
     * @return mixed
     */
    protected function getRedisIns($config = null)
    {
        if (empty(self::$coIns[Coroutine::getCid()])) {
            $pool = RedisPool::getIns($config);
            self::$coIns[Coroutine::getCid()] = $pool->get();
        }
        return self::$coIns[Coroutine::getCid()];
    }

    /**
     * 关闭连接
     */
    public function closeConnect()
    {

    }


    /**
     * @param $key
     * @return string
     */
    public function get($key)
    {
        return $this->redis()->get($key);
    }


    /**
     * @param $key
     * @param string $value
     * @param array $opt
     * @return mixed
     */
    public function set($key, string $value, $opt = null, null|string|int $expireTime = null)
    {
        $set = $this->redis()->set($key, $value, $opt);
        if (is_int($expireTime)) {
            $expireTime = time() + $expireTime;// mktime(23, 59, 59, date("m"), date("d"), date("Y"));
        } elseif (is_string($expireTime)) {
            $expireTime = is_numeric($expireTime) ? (int)$expireTime : strtotime($expireTime);
        }
        // 设置键的过期时间
        if (!empty($expireTime)) {
            $this->redis()->expireAt($key, $expireTime);
        }
        return $set;
    }


    /**
     * 增加值
     * @param $key
     */
    public function incr($key)
    {
        $this->redis()->incr($key);
    }

    /**
     * 增加值
     * @param $key
     */
    public function incrBy($key, $value)
    {
        $this->redis()->incrBy($key, $value);
    }


    /**
     * @param $key
     * @param $otherKeys
     * @return mixed
     */
    public function exists($key, $otherKeys = null)
    {
        return $this->redis()->exists($key);
    }


    /**
     * @param $key
     * @return mixed
     */
    public function del($key)
    {
        return $this->redis()->del($key);
    }


    /**
     * @param $key
     * @param $member
     * @return mixed
     */
    public function hExists($key, $member)
    {
        return $this->redis()->hExists($key, $member);
    }


    /**
     * @param $key
     * @param $member
     * @param $value
     * @return mixed
     */
    public function hSet($key, $member, $value)
    {
        return $this->redis()->hSet($key, $member, $value);
    }


    /**
     * @param $key
     * @param $member
     * @return mixed
     */
    public function hGet($key, $member)
    {
        return $this->redis()->hGet($key, $member);
    }


    /**
     * @param $key
     * @return mixed
     */
    public function hGetAll($key)
    {
        return $this->redis()->hGetAll($key);
    }


    /**
     * @param $key
     * @return mixed
     */
    public function hKeys($key)
    {
        return $this->redis()->hKeys($key);
    }


    /**
     * @param $key
     * @param $member
     * @return mixed
     */
    public function hDel($key, $member)
    {
        return $this->redis()->hDel($key, $member);
    }


    /**
     * 递增数量
     * @param string $key
     * @param string $member
     * @param int $value
     * @return mixed
     */
    public function hIncrBy($key, $member, $value)
    {
        return $this->redis()->hIncrBy($key, $member, $value);
    }

    /**
     * 返回哈希表 key 中，一个或多个给定域的值。
     * @param string $key
     * @param array $keys
     * @return mixed
     */
    public function hMGet($key, $keys)
    {
        return $this->redis()->hMGet($key, $keys);
    }


    /**
     * @param $key
     * @param $value
     * @return mixed
     */
    public function sAdd($key, $value)
    {
        return $this->redis()->sAdd($key, $value);
    }


    /**
     * @param $key
     * @param $value
     * @return mixed
     */
    public function sRem($key, $value)
    {
        return $this->redis()->sAdd($key, $value);
    }


    /**
     * @param $key
     * @param $value
     * @return mixed
     */
    public function sIsMember($key, $value)
    {
        return $this->redis()->sismember($key, $value);
    }


    /**
     * @param $key
     * @return mixed
     */
    public function sMembers($key)
    {
        return $this->redis()->sMembers($key);
    }


    /**
     * @param $key
     * @param $integer
     * @return mixed
     */
    public function expire($key, $integer)
    {
        return $this->redis()->expire($key, $integer);
    }
}
